package com.zkingsoft.services.sys.impl;

import com.zkingsoft.services.sys.SysDataDictionaryService;
import com.zkingsoft.sqlUtil.ServiceUtil;
import com.zkingsoft.pojo.PaginationVO;
import com.zkingsoft.model.sys.SysDataDictionary;
import com.zkingsoft.dao.sys.SysDataDictionaryDao;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

import com.zkingsoft.exception.GlobleException;
import com.zkingsoft.util.ModelUtils;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * This field was generated by Zking.software.Codegen.
 * @date 2016-11-16 15:25
 */
@Service("sysDataDictionaryService")
public class  SysDataDictionaryServiceImpl implements    SysDataDictionaryService  {

	
	@Autowired
	private SysDataDictionaryDao sysDataDictionaryDao;
	@Autowired
	private ServiceUtil serviceUtil;
	@Transactional(rollbackFor=Exception.class)
	@Override
	public int add(SysDataDictionary sysDataDictionary){
		if (serviceUtil.addCheckRepeat("sys_data_dictionary", "dic_name", 
				sysDataDictionary.getDicName())) {
			throw new GlobleException("字典数据已经存在");
		}
		int i=sysDataDictionaryDao.insert(sysDataDictionary);
		List<SysDataDictionary> list=sysDataDictionaryDao.selectByModel(sysDataDictionary);
		if(list!=null && list.size()>0){
			SysDataDictionary dic=list.get(0);
			dic.setDicCode(dic.getDicId().toString());
			sysDataDictionaryDao.updateByModel(dic);
		}
		return i;
	}
	
	@Override
	public int batchAdd(List<SysDataDictionary>  sysDataDictionaryList) {
		return sysDataDictionaryDao.batchInsert(sysDataDictionaryList);
	}
	
	
	@Transactional(rollbackFor=Exception.class)
    @Override
	public int modifyByMap(SysDataDictionary oldSysDataDictionary
	,SysDataDictionary newSysDataDictionary){
	if(!oldSysDataDictionary.getDicName().equals(newSysDataDictionary.getDicName())){
		if (serviceUtil.addCheckRepeat("sys_data_dictionary", "dic_name", 
				newSysDataDictionary.getDicName())) {
			throw new GlobleException("字典数据已经存在");
		}
	}
		if (!ModelUtils.isModified(oldSysDataDictionary, newSysDataDictionary)) {
			return 1;
		}
		Map<String, Object> modifyMap=null;
		try {
			modifyMap = ModelUtils.comparePojo2Map(oldSysDataDictionary, newSysDataDictionary);
		} catch (Exception e) {
			throw new GlobleException("数据对比失败");
		}
		if (modifyMap.size() > 0) {
			modifyMap.put("dicId", oldSysDataDictionary.getDicId());
			sysDataDictionaryDao.updateByMap(modifyMap);
		}
		return 1;
	}
	
	@Override
	public int modifyByModel(SysDataDictionary sysDataDictionary){
	
		return sysDataDictionaryDao.updateByModel(sysDataDictionary);
	
	}
	
	
	
	@Override
	public int remove(List<Long> list){
	
		return sysDataDictionaryDao.deleteByIds(list);
	
	}

	@Override
	public int removeById(Long dicId){
	
		return sysDataDictionaryDao.deleteById(dicId);
	
	}
	
	@Override
	public int removeByModel(SysDataDictionary sysDataDictionary){
	
		return sysDataDictionaryDao.deleteByModel(sysDataDictionary);
	
	}
	
	
	@Override
	public List<SysDataDictionary> findInPage(SysDataDictionary sysDataDictionary,  PaginationVO pageVo){
	
		return sysDataDictionaryDao.selectInPage(sysDataDictionary , pageVo);
	
	}
	
	@Override
	public List<SysDataDictionary> findByModel(SysDataDictionary sysDataDictionary){
	
		return sysDataDictionaryDao.selectByModel(sysDataDictionary);
	
	}
	
	@Override
	public int  findTotal(SysDataDictionary sysDataDictionary){
	
		return sysDataDictionaryDao.selectTotalRecord(sysDataDictionary);
	
	}
	
	@Override
	public SysDataDictionary  findById(Long dicId){
	
		return sysDataDictionaryDao.selectById(dicId);
	
	}

   	
	
	
}